//
//  ProfileTableViewCell.swift
//  GreenDeal
//
//  Created by Moennig Q on 6/12/2022.
//

import Foundation
import SnapKit
class ProfileTableViewCell : UITableViewCell {
    let colImageView = UIImageView()
    let colTitleLabel: ContentLabel = ContentLabel(R.font.interMedium(size: 15))
    let contentLabel: BaseLabel = BaseLabel()
    let lineView = View(hexStr: Constant.SeparatorColorHexStr)
    //
    var seperateViewHeightConstraint: Constraint?
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setUpviews()
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    func setUpviews(){
        self.selectionStyle = .none
        self.backgroundColor = .white
        
        //seperate view
        let seperateView = UIView()
        seperateView.backgroundColor = UIColor(hexStr: "#F5F5F5")
        self.contentView.addSubview(seperateView)
        seperateView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            self.seperateViewHeightConstraint = make.height.equalTo(0).constraint
        }
        //content label
        //抗压缩的优先级最高
        contentLabel.setContentCompressionResistancePriority(.required, for: .horizontal)
        contentLabel.textColor = UIColor(hexStr: "#63666A")
        contentLabel.setFont(R.font.interMedium(size: 14))
        contentLabel.textAlignment = .right
        contentLabel.numberOfLines = 0
        contentLabel.lineBreakMode = .byWordWrapping
        self.contentView.addSubview(contentLabel)
        contentLabel.snp.makeConstraints { (make) in
            make.right.equalTo(-20)
            make.top.equalTo(seperateView.snp.bottom).offset(5)
            make.height.greaterThanOrEqualTo(40)
            make.bottom.equalTo(-5)
        }
        //image view
        self.contentView.addSubview(colImageView)
        colImageView.snp.makeConstraints { (make) in
            make.centerY.equalTo(contentLabel)
            make.left.equalToSuperview().offset(20)
            make.width.height.equalTo(24)
        }
        //title label
        colTitleLabel.lineBreakMode = .byWordWrapping
        colTitleLabel.numberOfLines = 2
        self.contentView.addSubview(colTitleLabel)
        colTitleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(colImageView.snp.right).offset(12)
            make.right.equalTo(contentLabel.snp.left)
            make.width.greaterThanOrEqualTo(70 * Constant.currentSizeScale)
            make.height.greaterThanOrEqualTo(0)
            make.centerY.equalTo(contentLabel)
        }
        self.contentView.addSubview(lineView)
        lineView.snp.makeConstraints { (make) in
            make.left.equalTo(colTitleLabel)
            make.bottom.equalToSuperview()
            make.height.equalTo(1)
            make.right.equalTo(contentLabel)
        }
    }
    func setData(_ viewModel: ProfileViewModel){
        self.colImageView.image = UIImage(named: viewModel.imageName)
        self.colTitleLabel.text = viewModel.title
        self.contentLabel.text = viewModel.content
        self.seperateViewHeightConstraint?.update(offset: viewModel.seperateHeight)
        self.lineView.isHidden = viewModel.hideLineViewFlag
    }
}
